//package net.yla.smartbus.core.asp;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import lombok.extern.slf4j.Slf4j;
//import net.yla.smartbus.core.base.BaseController;
//import net.yla.smartbus.core.util.SqlTableUtil;
//import net.yla.smartbus.core.util.SqlUtils;
//import net.yla.smartbus.module.ba.log.entity.LogDO;
//import net.yla.smartbus.module.ba.log.service.LogService;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Arrays;
//import java.util.List;
//
///**
// * 记录前端请求的信息
// *
// * @author ZengXiaoLiang
// */
//@Aspect
//@Order(6)
//@Component
//@Slf4j
//public class WebLogAspect extends BaseController {
//
//
//    @Autowired
//    LogService logService;
//
//    @Autowired
//    private SqlSessionFactory sqlSessionFactory;
//
//    ThreadLocal<Long> startTime = new ThreadLocal<>();
//
//    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
//
//    /**
//     * @WebLogInfo注解切点
//     */
//    @Pointcut(value = "@annotation(net.yla.smartbus.core.validation.WebLogInfo)")
//    public void webLog() {
//
//    }
//
//    /**
//     * controller层切点
//     */
//    @Pointcut("execution(public * net.yla.smartbus.module.controller..*.*(..))")
//    public void controllerLog() {
//
//    }
//
//    /**
//     * mapper层切点 排除LogDDOMapper类增加日志方法，防止出现死循环
//     */
//    @Pointcut("execution(public * net.yla.smartbus.module.ba.*.mapper..*.*(..))&&!execution(public * net.yla.smartbus.module.ba.log.mapper.LogDOMapper.insertLog(..))")
//    public void mapperLog() {
//
//    }
//
//    /**
//     * controller层前置方法
//     *
//     * @param joinPoint
//     */
//    @Before("mapperLog(),controllerLog()")
//    public void dobefroeService(JoinPoint joinPoint) throws Exception {
//        log.info("日志记录请求...");
//        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String url = request.getRequestURL().toString();
//        String sql = SqlUtils.getMybatisSql((ProceedingJoinPoint) joinPoint, sqlSessionFactory);
//        //获取方法参数
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        if (sql != null && !sql.equals("")) {
//            List<String> tableNames = SqlTableUtil.getTableNames(sql);
//            String tables = StringUtils.join(tableNames, ",");
//            String[] split = sql.split(" ");
//            String opStr = split[0];
//            Integer op = null;
//            if (opStr.equalsIgnoreCase("insert")) {
//                op = 1;
//            } else if (opStr.equalsIgnoreCase("update")) {
//                op = 2;
//            } else if (opStr.equalsIgnoreCase("delete")) {
//                op = 3;
//            } else if (opStr.equalsIgnoreCase("select")) {
//                op = 0;
//            }
//            String form = "";
//            if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
//                for (int i = 0; i < joinPoint.getArgs().length; i++) {
//                    form += JSON.toJSONString(joinPoint.getArgs()[i]) + ";";
//                }
//            }
//            log.info("form:" + form);
//            LogDO logDo = new LogDO();
//            logDo.setAdminId(1);
//            logDo.setAfter(form);
//            logDo.setDataRowId(2);
//            logDo.setSql(sql);
//            logDo.setOp(op);
//            logDo.setBefore("不知道");
//            logDo.setTableName(tables);
//            logService.insertLog(logDo);
//        }
//
//        //类名
////        String className = joinPoint.getTarget().getClass().getName();
////        String simpleClassName = joinPoint.getTarget().getClass().getSimpleName();
//        //请求方法
////        String method = joinPoint.getSignature().getName() + "()";
//        //方法参数
//        //方法描述
//        //获取用户请求方法的参数并序列化为JSON格式字符串
//
//    }
//
//
//    @After("webLog()")
//    @Transactional
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        startTime.set(System.currentTimeMillis());
//        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String ip = request.getRemoteAddr();
//        // 记录下请求内容
//        logger.info("URL : " + request.getRequestURL().toString());
//        logger.info("HTTP_METHOD : " + request.getMethod());
//        logger.info("IP : " + (ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip));
//        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//    }
//
//
//    @AfterReturning(returning = "ret", pointcut = "webLog()")
//    public void doAfter(Object ret) throws Throwable {
//        // 处理完请求，返回内容
//        logger.info("RESPONSE : " + ret);
//        logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));
//    }
//
//
//    /**
//     * 定义一个切点,捕获运营系统登录
//     */
//
//    @Pointcut(value = "@annotation(net.yla.smartbus.core.validation.OcLogInfo)")
//    public void ocLogin() {
//
//    }
//}
